<?php
/*** 规范：//功能说明 [参数说明] 开发者***/

//取得IP liuxin
function getuserip(){
		$user_IP = !empty($_SERVER["HTTP_VIA"]) ? $_SERVER["HTTP_X_FORWARDED_FOR"] : $_SERVER["REMOTE_ADDR"];
		$user_IP = !empty($user_IP) ? $user_IP : $_SERVER["REMOTE_ADDR"]; 	
		return $user_IP;
}


//数组键值排序 [$sort特定键值 $order(desc||asc)] liuxin
function arrsort($arr,$sort,$order)
{
	foreach($arr as $k=>$v)
	{
		$temparr[$k]=$arr[$k][$sort];
	}
	if($order=="desc")
	{
		arsort($temparr);
	}else{
		asort($temparr);
	}
	foreach($temparr as $k=>$v)
	{
		$retuarr[]=$arr[$k];
	}
	return $retuarr;
	
}

//随机字符串 [num=输出字符数] liuxin
function getrandstr($num){
	$str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";//输出字符集
	$len = strlen($str)-1;
	for($i=0 ; $i<$num; $i++){
		$s .= $str[rand(0,$len)];
	}
	return $s;
}



//任何数字得到双精度小数 liuxin
function isdecimal($number){  
	if(preg_match('/^[-\+]?\d+(\.\d+)?$/',$number)){  
		return $number;
	}else{  
		return doubleval($number);
	}  
}

//返回加过水印图片[$imgSrc原图地址 markimg水印地址 marktype=img||text marktext 文字水印内容 textcolor文字水印颜色 fonttype文字水印字体文件 markpos=位置0：随机位置，1顶部居左 2顶部居中 3顶部居右 4左边居中 5图片中心 6右边居中 7底部居左 8底部居中 9底部居右] liuxin
function getmakepic($imgSrc,$markImg,$markType='img',$markPos=0,$markText='',$TextColor='',$fontType='')
{
	$srcInfo = getimagesize($imgSrc);
	$srcImg_w    = $srcInfo[0];
	$srcImg_h    = $srcInfo[1];
	switch ($srcInfo[2])
	{
		case 1:
			$srcim =imagecreatefromgif($imgSrc);
			break;
		case 2:
			$srcim =imagecreatefromjpeg($imgSrc);
			break;
		case 3:
			$srcim =imagecreatefrompng($imgSrc);
			break;
		default:
			die("不支持的图片文件类型");
			exit;
	}
	if($markType=='img')
	{
		if(empty($markImg))
		{
			return;
		}
		$markImgInfo = getimagesize($markImg);
		$markImg_w    = $markImgInfo[0];
		$markImg_h    = $markImgInfo[1];
		   
		if($srcImg_w < $markImg_w || $srcImg_h < $markImg_h)
		{
			return;
		}
		   
		switch ($markImgInfo[2])
		{
			case 1:
				$markim =imagecreatefromgif($markImg);
				break;
			case 2:
				$markim =imagecreatefromjpeg($markImg);
				break;
			case 3:
				$markim =imagecreatefrompng($markImg);
				break;
			default:
				die("不支持的水印图片文件类型");
				exit;
		}
		   
		$logow = $markImg_w;
		$logoh = $markImg_h;
	}
	if($markType=='text')
	{
		$fontSize = 12;
		if(!empty($markText))
		{
			if(empty($fontType))
			{
				$fontType='arial.ttf';
			}	
			
		}
		else {
			return;
		}
		   
		$box = imagettfbbox($fontSize, 0, $fontType,$markText);
		$logow = max($box[2], $box[4]) - min($box[0], $box[6]);
		$logoh = max($box[1], $box[3]) - min($box[5], $box[7]);
	}
	if($markPos == 0)
	{
		$markPos = rand(1, 9);
	}
	   
	switch($markPos)
	{
		case 1:
			$x = +5;
			$y = +5;
			break;
		case 2:
			$x = ($srcImg_w - $logow) / 2;
			$y = +5;
			break;
		case 3:
			$x = $srcImg_w - $logow - 5;
			$y = +15;
			break;
		case 4:
			$x = +5;
			$y = ($srcImg_h - $logoh) / 2;
			break;
		case 5:
			$x = ($srcImg_w - $logow) / 2;
			$y = ($srcImg_h - $logoh) / 2;
			break;
		case 6:
			$x = $srcImg_w - $logow - 5;
			$y = ($srcImg_h - $logoh) / 2;
			break;
		case 7:
			$x = +5;
			$y = $srcImg_h - $logoh - 5;
			break;
		case 8:
			$x = ($srcImg_w - $logow) / 2;
			$y = $srcImg_h - $logoh - 5;
			break;
		case 9:
			$x = $srcImg_w - $logow - 5;
			$y = $srcImg_h - $logoh -5;
			break;
		default:
			die("此位置不支持");
			exit;
	}
	$dst_img = imagecreatetruecolor($srcImg_w, $srcImg_h);
	imagecopy ($dst_img,$srcim,0,0,0,0,$srcImg_w,$srcImg_h);
	if($markType=="img")
	{
		imagecopy($dst_img, $markim, $x, $y, 0, 0, $logow, $logoh);
		imagedestroy($markim);
	}
	if(!strcmp($markType,"text"))
	{
		$rgb = explode(',',$TextColor);
		$color = imagecolorallocate($dst_img, $rgb[0], $rgb[1], $rgb[2]);
		imagettftext($dst_img, $fontSize, 0, $x, $y, $color, $fontType,iconv("gbk","utf-8",$markText));
	}
	$pictemurl=IMG_CACHE_DIR.IMG_DIR.rand(1000,9999);
	switch ($srcInfo[2])
	{
		case 1:
			imagegif($dst_img,$pictemurl.".gif",80);
		$repic=$pictemurl.".gif";
		$hz='.gif';
		break;
		case 2:
			imagejpeg($dst_img,$pictemurl.".jpg",80);
		$repic=$pictemurl.".jpg";
		$hz='.jpg';
		break;
		case 3:
			imagepng($dst_img,$pictemurl.".png",80);
		$repic=$pictemurl.".png";
		$hz='.png';
		break;
		default:
			die("不支持的水印图片文件类型");
			exit;
	}
	imagedestroy($dst_img);
	imagedestroy($srcim);
	return $repic;
}

//返回JS提示 [alert提示内容] liuxin
function jsfshow($str){
	return "<script>alert('".$str."')</script>";
}
	
//返回JS方法 [funct=调用方法名 parm=参数 空为无参数 单参数直接写 多参数为一维数组] liuxin
function jsffunc($func,$parm=''){
if(is_array($parm)){
		$p='';
		foreach($parm as $v){
			if(is_int($v) || is_numeric($v)){
				$p.=$v.",";
			}else{
				$p.="'".$v."',";
			}
		}
	}elseif(!empty($parm)){
		$p=$parm;
	}
	return "<script>{$func}({$p})</script>";
}